package com.xiaojian.blog.controller.admin;

import com.github.pagehelper.PageInfo;
import com.xiaojian.blog.constant.WebConstant;
import com.xiaojian.blog.controller.BaseController;
import com.xiaojian.blog.dto.Types;
import com.xiaojian.blog.model.AttachPO;
import com.xiaojian.blog.service.AttachService;
import com.xiaojian.blog.service.LogService;
import com.xiaojian.blog.utils.Commons;
import com.xiaojian.blog.utils.TaleUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 附件管理控制器
 */
@Controller
@RequestMapping("admin/attach")
public class AttachController extends BaseController {
    private static final Logger LOGGER = LoggerFactory.getLogger(AttachController.class);

    public static final String CLASSPATH = TaleUtils.getUploadFilePath();

    @Resource
    private AttachService attachService;

    @Resource
    private LogService logService;

    /**
     * 附件页面
     */
    @GetMapping(value = "")
    public String index(HttpServletRequest request,
                        @RequestParam(value = "page", defaultValue = "1") int page,
                        @RequestParam(value = "limit", defaultValue = "12") int limit) {
        PageInfo<AttachPO> attachs = attachService.getAttachs(page, limit);
        request.setAttribute("attachs", attachs);
        request.setAttribute(Types.ATTACH_URL.getType(), Commons.site_option(Types.ATTACH_URL.getType(), Commons.site_url()));
        request.setAttribute("max_file_size", WebConstant.MAX_FILE_SIZE /1024);
        return "admin/attach";
    }
}
